package com.bgs.engineering.api.sysUser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bgs.engineering.api.sysUser.service.SysUserService;
import com.bgs.engineering.util.ObjectUtils;
import com.bgs.engineering.util.pageUtil.MybatisPageHelper;
import com.bgs.engineering.util.pageUtil.PageRequest;
import com.bgs.engineering.util.pageUtil.PageResult;
import com.bgs.engineering.util.validator.ValidatorUtil;
import com.bgs.engineering.api.sysUser.entiy.SysUser;
import com.bgs.engineering.api.sysUser.mapper.SysUserMapper;
import com.bgs.engineering.api.sysUser.model.SysUserModel;
import com.bgs.engineering.common.errorCode.CommonException;
import com.bgs.engineering.common.errorCode.EmErrorCode;
import com.bgs.engineering.common.response.CommonResult;
import com.bgs.engineering.util.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName SysUserServiceImpl
 * @Description 用户管理服务类 用于实现用户的增删改查 登录 注销
 * @Author zcy
 * @Date 2023/3/24 16:15
 **/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

  @Resource
  SysUserMapper sysUserMapper;
  @Autowired
  BCryptPasswordEncoder bCryptPasswordEncoder;

  /**
   * 通过登录名获取用户信息
   *
   * @param loginName
   * @return
   */
  @Override
  public SysUser getUserByLoginName(String loginName) {
    // 判断用户登录名是否为空
    if (ObjectUtils.isNotEmpty(loginName)) {
      // 查询登录人信息
      /*LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(SysUser::getLoginName, loginName);*/
      return sysUserMapper.getUserByLoginName(loginName);

    } else {
      return null;
    }
  }

  /**
   * 通过邮箱查询用户
   *
   * @param emailAccount
   * @return
   */
  @Override
  public SysUser getUserByEmailAccount(String emailAccount) {
    // 判断用户登录名是否为空
    if (ObjectUtils.isNotEmpty(emailAccount)) {
      // 使用Example表达式查询登录人信息
      LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(SysUser::getEmail, emailAccount);
      return sysUserMapper.selectOne(queryWrapper);
    } else {
      return null;
    }
  }

  /**
   * 更新登录成功后更新用户信息
   *
   * @param sysUser
   */
  @Override
  public void updateUserByLogin(SysUser sysUser) {
    if (ObjectUtils.isNotEmpty(sysUser) && ObjectUtils.isNotEmpty(sysUser.getId())) {
      sysUserMapper.updateById(sysUser);
    }
  }

  /**
   * 新增用户
   *
   * @param sysUserModel
   * @return
   * @throws CommonException
   */
  @Override
  public CommonResult saveUser(SysUserModel sysUserModel) throws CommonException {
    // 验证参数的规范性
    if (ValidatorUtil.ValidationObject(sysUserModel)) {
      SysUser sysUser = new SysUser();
      // sysUserModel 对象拷贝成 sysUser
      BeanUtils.copy(sysUserModel, sysUser);
      // 设置密码
      sysUser.setPassword(bCryptPasswordEncoder.encode(sysUser.getPassword()));
      int i = sysUserMapper.insert(sysUser);
      if (i > 0) {
        return CommonResult.ok(EmErrorCode.SAVE_OK);
      } else {
        return CommonResult.ok(EmErrorCode.SAVE_ERROR);
      }
    } else {
      return CommonResult.ok(EmErrorCode.SAVE_ERROR);
    }
  }

  /**
   * 修改用户信息
   *
   * @param sysUserModel
   * @return
   */
  @Override
  public CommonResult updateUser(SysUserModel sysUserModel) throws CommonException {
    // 验证参数的规范性
    if (ValidatorUtil.ValidationObject(sysUserModel)) {
      SysUser sysUser = new SysUser();
      // sysUserModel 对象拷贝成 sysUser
      BeanUtils.copy(sysUserModel, sysUser);
      int i = sysUserMapper.updateById(sysUser);
      if (i > 0) {
        return CommonResult.ok(EmErrorCode.UPDATE_OK);
      } else {
        return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
      }
    } else {
      return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
    }
  }

  /**
   * 删除用户
   *
   * @param id
   * @return
   */
  @Override
  public CommonResult delUserById(Integer id) throws CommonException {
    // 判断是否id为空
    if (ObjectUtils.isNotEmpty(id)) {
      int i = sysUserMapper.deleteById(id);
      // 判断是否操作成功
      if (i > 0) {
        return CommonResult.ok(EmErrorCode.DELETE_OK);
      } else {
        return CommonResult.ok(EmErrorCode.DELETE_ERROR);
      }
    } else {
      throw new CommonException(EmErrorCode.ID_NULL);
    }
  }

  /**
   * 分页查询
   *
   * @param sysUserModel
   * @param pageRequest
   * @return
   */
  @Override
  public CommonResult getUserByPage(SysUserModel sysUserModel, PageRequest pageRequest) {
    // 设置分页
    MybatisPageHelper.startPage(pageRequest);
    LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
    // 设置模糊查询
    if (ObjectUtils.isNotEmpty(sysUserModel.getUserName())) {
      queryWrapper.like(SysUser::getUserName, sysUserModel.getUserName());
    }
    PageResult pageResult = MybatisPageHelper.getPageResult(sysUserMapper.selectList(queryWrapper));
    return CommonResult.ok(pageResult);
  }

  /**
   * 根据手机号查询用户信息
   *
   * @param phoneAccount
   * @return
   */
  @Override
  public SysUser getUserByPhoneAccount(String phoneAccount) {
    // 判断用户登录名是否为空
    if (ObjectUtils.isNotEmpty(phoneAccount)) {
      // 使用Example表达式查询登录人信息
      /*Example example = new Example(SysUser.class);
      example.createCriteria().andEqualTo("phone", phoneAccount);*/
      LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(SysUser::getPhone, phoneAccount);
      return sysUserMapper.selectOne(queryWrapper);
    } else {
      return null;
    }
  }
}
